package com.specialized.orders.consumer;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.specialized.orders.common.domain.PickAirport;
import com.specialized.orders.service.IPickAirportService;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author：djh
 * @Package：com.specialized.orders.consumer
 * @Project：Specialized-Cloud
 * @name：AirPortConsumer
 * @Date：2024/7/29 10:32
 */
@Component
@Log4j2
public class AirPortConsumer {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    private IPickAirportService pickAirportService;


    @RabbitListener(queuesToDeclare = @Queue(value = "insertPickAir"))
    public void insertAirport(String airport, Channel channel, Message message){
        PickAirport pickAirport = JSONObject.parseObject(airport, PickAirport.class);
        String messageId = message.getMessageProperties().getMessageId();
        try {
            Long add = redisTemplate.opsForSet().add("insertpic", messageId);
            if (add>0){
                PickAirport update = new PickAirport();
                update.setOrderCard(pickAirport.getOrderCard());
                update.setOrderStatus(2L);
                update.setDriverId(132L);
                pickAirportService.updatePickAirport(update);
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            }
        } catch (IOException e) {
            Boolean delete = redisTemplate.delete("insertpic");
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }


    }


}
